package com.jd.fruit.common.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;

@SuppressWarnings("serial")
public abstract class BaseModel<M extends Model<M>> extends Model<M> {
	
	// ---------------------------------- <<< SQL METHOED BEGIN >>> ----------------------------------
	
	/**
	 * 根据填充的值查询相应的LIST
	 * @return
	 */
	public List<M> list(){
		String sql = "SELECT * " + getQuerySql();
		Object[] params = this.whereParams.toArray();
		return find(sql,params);
	}
	
	/**
	 * 根据填充的值查询相应的LIST
	 * @return
	 */
	public List<M> listByCache(){
		String sql =  "SELECT * " +getQuerySql();
		Object[] params = whereParams.toArray();
		String tableName = this.getTable().getName();
		String cacheName = tableName;
		String keyName = "";
		return this.findByCache(cacheName, keyName, sql, params);
	}
	
	/**
	 * 分页查询
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<M> list(int pageNumber,int pageSize){
		String whereSql = getQuerySql();
		Object[] params = this.whereParams.toArray();
		return this.paginate(pageNumber, pageSize,"SELECT * " ,whereSql,params);
	}
	
	/**
	 * 分页查询
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<M> listByCache(int pageNumber,int pageSize){
		String whereSql = getQuerySql();
		String tableName = this.getTable().getName();
		Object[] params = this.whereParams.toArray();
		String cacheName = tableName + "_page";
		String cacheKey = this.getCacheKey();
		return paginateByCache(cacheName, cacheKey, pageNumber, pageSize, "SELECT * ", whereSql, params);
	}
	
	
	
	
	// ---------------------------------- <<< SQL METHOED END >>> ----------------------------------
	
	
	protected List<Object> whereParams = new ArrayList<Object>();
	private StringBuffer whereSql = new StringBuffer();
	private String orderByStr = "";
	
	
	/**
	 * 获得查询的SQL
	 * @return
	 */
	public String getQuerySql(){
		whereSql = new StringBuffer();
		whereParams.clear();
		initWhere();
		String tableName = this.getTable().getName();
		String sql = " FROM " + tableName  +" WHERE 1=1 "+ whereSql.toString() + " " +orderBy() ;
		return sql;
	}
	
	
	/**
	 * 获取表映射对象
	 * @return
	 */
	public Table getTable() {
		return TableMapping.me().getTable(getClass());
	}
	
	/**
	 * 拼接SQL WHERE
	 * @return
	 */
	public abstract void initWhere();
	/**
	 * 拼接SQL
	 * @param op
	 * @param columnName
	 * @param compareOP
	 * @param columnValue
	 */
	public void appendSql(String logic,String columnName,String compareSql,Object columnValue){
		whereSql.append(" ").append(logic).append(" ").append(columnName).append(" ").append(compareSql).append(" ? ");
		whereParams.add(columnValue);
	}
	
	/**
	 * 排序
	 * @return
	 */
	public String orderBy(){
		return orderByStr;
	}
	
	/**
	 * 获得缓存的Key,这个Key
	 * @return
	 */
	public String getCacheKey(){
		StringBuffer cacheKey = new StringBuffer(whereSql + " AND AAA=?");
		Object[] params = this.whereParams.toArray();
		for(Object param : params){
			int index = cacheKey.indexOf("?");
			if(index>=0){
				cacheKey.replace(index, (index+1), "'" + param.toString()+ "'");
			}
		}
		return cacheKey.toString();
	}
	
}
